package com.skyvin.nreceipts.admin.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skyvin.nreceipts.admin.mongorepository.ItemCategoryRepository;
import com.skyvin.nreceipts.admin.service.ReceiptItemCategoryService;
import com.skyvin.nreceipts.common.controller.BaseController;
import com.skyvin.nreceipts.common.document.ReceiptItemCategory;
import com.skyvin.nreceipts.common.value.JQGridResponse;
import com.skyvin.nreceipts.common.value.MessageValue;

@Controller
@RequestMapping(value="itemCategory")
public class ItemCategoryController extends BaseController {
	private static Logger logger = Logger.getLogger(ItemCategoryController.class);
	
	@Autowired
	private ItemCategoryRepository itemCategoryRepository; 
	
	@Autowired
	private ReceiptItemCategoryService receiptItemCategoryService;
	
	@Autowired
	private MongoTemplate mongoTemplate;

	@RequestMapping(value="/loadItemCategoryPage", method=RequestMethod.GET)
	public String loadReceiptItemCategory() {
		return "admin/receiptItemCategoryEdit";
	}
	
	@RequestMapping(value="/viewItemCategories", method=RequestMethod.GET)
	@ResponseBody
	public JQGridResponse viewReceiptItemCategories(@RequestParam("_search") Boolean search,
    		@RequestParam(value="filters", required=false) String filters,
    		@RequestParam(value="page", required=false) Integer page,
    		@RequestParam(value="rows", required=false) Integer rows,
    		@RequestParam(value="sidx", required=false) String sidx,
    		@RequestParam(value="sord", required=false) String sord) {
		
		List<ReceiptItemCategory> receiptItemCategoryList = null;
		//Page<ReceiptItemCategory> ReceiptItemCategoryPageList = null;
		
		JQGridResponse jqGridResponse = new JQGridResponse();
		Pageable pageRequest = new PageRequest(page-1, rows);
		long count = 0;
		int countOfReceiptItemCategoryListReturned = 0;
		
		//ReceiptCategoryPageList = ReceiptCategoryRepository.findAll(pageRequest);
		Query query = new Query();
		query.addCriteria(Criteria.where("userPID").is(getCurrentUserPID()));
		query.skip((page-1) * rows);
		query.limit(rows);
		
		Query countQuery = new Query();
		countQuery.addCriteria(Criteria.where("userPID").is(getCurrentUserPID()));
		
		receiptItemCategoryList = mongoTemplate.find(query, ReceiptItemCategory.class, receiptItemCategoryService.getMyCollection(ReceiptItemCategory.class));
		if(receiptItemCategoryList!= null) {
			countOfReceiptItemCategoryListReturned = receiptItemCategoryList.size();
		}
		count = mongoTemplate.count(new Query(), receiptItemCategoryService.getMyCollection(ReceiptItemCategory.class));
		logger.info("Count : " + count);
		logger.info("Records retrieved : " + countOfReceiptItemCategoryListReturned);
		//receiptCategoryList = receiptCategoryPageList.getContent();
		//receiptCategoryDTOList = ReceiptCategoryMapper.map(receiptCategoryPageList);
		
		jqGridResponse.setPage(1);
		
		if(receiptItemCategoryList!= null) {
			
			jqGridResponse.setRecords(countOfReceiptItemCategoryListReturned);
			jqGridResponse.setTotal((int) count);
			jqGridResponse.setPage(page);
		}
		jqGridResponse.setRows(receiptItemCategoryList);
		return jqGridResponse;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MessageValue save(ReceiptItemCategory receiptItemCategory) {
		logger.info("Receipt Item category passed: " + receiptItemCategory.getId() + "-name :" + receiptItemCategory.getValue());
		MessageValue message = new MessageValue();
		receiptItemCategory.setUserPID(getCurrentUserPID());
		
		receiptItemCategoryService.save(receiptItemCategory);
		
		message.setMessage("File is uploaded...");
		//message.setDetailedMessage("http://farm4.staticflickr.com/3510/3208351775_e06db18ee4.jpg");

		return message;
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public @ResponseBody MessageValue deleteReceiptItemCategory(@RequestParam("oper") String oper, @RequestParam("id") String receiptItemCategoryPID) {
		MessageValue message = new MessageValue();
		receiptItemCategoryService.deleteMyReceiptItemCategory(receiptItemCategoryPID);
		message.setMessage("Receipt Category deleted");
		return message;
	}
	
	@RequestMapping(value="/searchItemCategories", method=RequestMethod.GET)
	@ResponseBody
	public List<ReceiptItemCategory> searchItemCategories(@RequestParam(value="term", required=false) String searchTerm) {
		List<ReceiptItemCategory> receiptItemCategories = receiptItemCategoryService.findMyCategories(searchTerm);
		return receiptItemCategories;
	}
}
